<?php

namespace App\Traits;

use App\Services\Api\Api;

trait DeviceTrait
{
    public function getDeviceList($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
            'name' => $data['device_name'],
            'domain' => $data['domain2'],
            'authStatus' => $data['status'],
            'pageNo' => $data['page_number'],
            'pageSize' => $data['page_limit'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return ['dataList' => $result['devices'], 'pageCount' => $result['pageTotal']];
        }
        return false;
    }

    public function getDeviceInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'devId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return [
                'device_name' => $result['name'],
                'device_number_list_th' => $result['th'],
                'device_number_list_td' => $result['td'],
                'brand_name' => $result['brand'],
                'type_name' => $result['type'],
                'img_list' => $result['pic'],
                'device_desc' => $result['intro'],
            ];
        }
        return false;
    }

    public function getDeviceInfoByEdit($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'devId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return [
                'device_name' => $result['name'],
                'type_id' => $result['type'],
                'brand_id' => $result['brandId'],
                'img_list' => $result['pic'],
                'device_desc' => $result['intro'],
            ];
        }
        return false;
    }

    public function addCompanyDeviceInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'devId' => -1,
            'custId' => $data['company_id'],
            'name' => $data['device_name'],
            'type' => $data['type_id'],
            'brand' => $data['brand_id'],
            'vbrand' => '',
            'pic' => $data['img_list'],
            'intro' => $data['device_desc'],
            'param' => [],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return $result['devId'];
        }
        return false;
    }

    public function editCompanyDeviceInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'devId' => $data['id'],
            'custId' => $data['company_id'],
            'name' => $data['device_name'],
            'type' => $data['type_id'],
            'brand' => $data['brand_id'],
            'vbrand' => '',
            'pic' => $data['img_list'],
            'intro' => $data['device_desc'],
            'param' => [],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function checkCompanyDeviceInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'devId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function delCompanyDeviceInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'devId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function addCompanyDeviceNumberInfo($apiCode, $data)
    {
        $obj = new Api();
        $arrParamList = explode(',', $data['param_list_info']);
        $arrParam = [];
        for ($i = 0; $i < count($arrParamList); $i++) {
            if ($i != 0 && $i != count($arrParamList) - 1) {
                array_push($arrParam, $arrParamList[$i]);
            }
        }
        $name = $arrParamList[0];
        $price = $arrParamList[count($arrParamList) - 1];
        $formData = [
            'custId' => $data['company_id'],
            'devId' => $data['device_id'],
            'modelId' => -1,
            'name' => $name,
            'paramV' => $arrParam,
            'price' => $price,
            //'intro' => '',
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function editCompanyDeviceNumberInfo($apiCode, $data)
    {
        $obj = new Api();
        $arrParamList = explode(',', $data['param_list_info']);
        $arrParam = [];
        for ($i = 0; $i < count($arrParamList); $i++) {
            if ($i != 0 && $i != count($arrParamList) - 1) {
                array_push($arrParam, $arrParamList[$i]);
            }
        }
        $name = $arrParamList[0];
        $price = $arrParamList[count($arrParamList) - 1];
        $formData = [
            'custId' => $data['company_id'],
            'devId' => $data['device_id'],
            'modelId' => $data['id'],
            'name' => $name,
            'paramV' => $arrParam,
            'price' => $price,
            //'intro' => '',
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function delCompanyDeviceNumberInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'devId' => $data['device_id'],
            'custId' => $data['company_id'],
            'modelId' => $data['device_number_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }
}
